//
//  HamatonQRScanViewController.swift
//  hamaton-app
//
//  Created by intyt on 2024/3/26.
//

import UIKit

class HamatonQRScanViewController: UIViewController {

    // MARK:- 事件回调
    // MARK: 结果回掉

    // MARK:- 懒加载
    // MARK: 扫描页面懒加载
    lazy var scanView: HamatonQRCodeBaseView = {
        let scanView = HamatonQRCodeBaseView.init(frame: UIScreen.main.bounds)
        return scanView
    }()

    // MARK: 扫描管理器懒加载
    lazy var qrcodeScanner: HamatonQRCodeScanner = {
        let scanner = HamatonQRCodeScanner()
        scanner.qrCodeDelegate = self
        return scanner
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = UIColor.black
        view.addSubview(scanView)
        setupClosure()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(true, animated: animated)
        qrcodeScanner.gridStartRunningSession()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.setNavigationBarHidden(false, animated: animated)
        qrcodeScanner.gridStopRunningSession()
    }

    // MARK:- 回调响应
    private func setupClosure() {
        weak var weakSelf = self
        scanView.backAction = { (Bool) -> Void in
            weakSelf?.backAction()
        }
        scanView.albumAction = { (Bool) -> Void in
            weakSelf?.qrcodeScanner.HamatonQRCodeWithImagePicker(weakSelf)
        }
        scanView.lightAction = { (lightOpen: Bool) -> Void in
            HamatonQRCodeScanner.turnOnTorchLight(lightOpen)
        }
        HamatonQRCodeScanner.cameraAuth { (granted: Bool) in
            if granted == true {
                DispatchQueue.main.async {
                    weakSelf?.qrcodeScanner.gridQRCode(weakSelf!.view, CGRect(x: 0, y: 0, width: 1, height: 1))
                }
            } else {
                print("请开启相机权限")
                // KNBProgressHUD.shared.show("请开启相机权限")
                self.view.makeToast("请开启相机权限", duration: 3.0, position: .bottom)
            }
        }
    }
    
    @objc fileprivate func backAction() {
        if navigationController?.popViewController(animated: false) == nil {
            dismiss(animated: true, completion: nil)
        }
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension HamatonQRScanViewController: HamatonQRCodeScannerDelegate {
    
    func gridQRCodeScanResult(_ resultStr: String?, _ errorCode: Int, _ errorMessage: String) {
        if errorCode == -1 {

        } else if errorCode == -2 {
//            KNBProgressHUD.shared.show("未发现有效二维码")
            print("未发现有效二维码")
        } else if errorCode == 0 {
            print("\(String(resultStr!))")
        }
    }

    func gridQRCodeBrightnessValue(_ brightnessValue: Double) {
        DispatchQueue.main.async {
            print("光强brightnessValue:\(brightnessValue)")
            if brightnessValue>0 && self.scanView.lightHidden==false && self.scanView.lightSelect==false {
                self.scanView.lightHidden = true
            } else if brightnessValue<0 && self.scanView.lightHidden==true {
                self.scanView.lightHidden = false
            } else {
                print("其他状态维持原样，如开灯即使光强大于0也不能隐藏")
            }
        }
    }
}
